<!--API开通弹窗-->
<template>
  <div class="bg">

    <div class="content bg3" v-if="api">
      <text class="title font34 text1">{{ T('开通&{1}账户').replace("&{1}", api.exchangeName)}}</text>

      <text class="font28 text1 text">{{T('为了您能够正常使用本平台功能，现需要开通以下账户：')}}</text>

      <text class="font28 text1 label mb40">{{T('·现货账户')}}</text>
      <!-- <text class="font28 text1 label mb40">{{T('·合约账户')}}</text> -->

      <!-- <div class="agrees" >
        <LImage v-if="isOk" class="img-agree" :src="Config.img('api_yc_yes.png')" @click="isOk = !isOk"></LImage>
        <LImage v-else class="img-agree" :src="Config.img('api_yc_no.png')" @click="isOk = !isOk"></LImage>
        <div class="column">
          <div class="row acenter">
            <text class="font24 text1" @click="isOk = !isOk">{{T('我已阅读并同意')}}</text>
            <text class="font24 main-t1" @click="opJyClick">{{T('《交易开户协议》')}}</text>
            <text class="font24 text1">{{T('、')}}</text>
          </div>
          <text class="agree-text font24 main-t1" @click="opLHClick">{{T('《量化开户协议》')}}</text>
        </div>
      </div> -->
     

      <div class="buts">
        <div class="but" @click="cancelClick">
          <text class="font32 text2">{{T('取消')}}</text>
        </div>
        <div class="line"></div>
        <div class="but" @click="okClick">
          <text class="font32 main-t1">{{T('一键开通')}}</text>
        </div>
      </div>
    </div>
  </div>
</template>

<script>

import jnavigator from "@/_js/navigator"; 
import jconfig from "@/_js/config"; 
import jmodal from "@/_js/modal"; 
import jhttps from "@/_js/https"; 
import japi from "@/_js/api"; 

const weexFunction = weex.requireModule("WeexFunction");

import I18N from "@/_i18n/i18n";
var T = I18N.t;

export default {
  data() {
    return {
      T:T,
      Config: jconfig,
      isOk:true,

      api:undefined,
    };
  },

  mounted() {
    var param = weex.config;
    this.api = param.api;
    weexFunction.debugLog(this.api);
  },

  methods: {

    /********************************** 点击触发*/
    //http获取账户apiKey
    httpCrerateAddress(){

      jmodal.showLoad();
      var param = {};
      jhttps.post("uniswap/crerateAddress",param,true,(data)=>{

        jmodal.hideLoad();
        if(data.status == 200){
          var data = data.data;
          this.api.param.forEach((item)=>{
            item.value = data.address;
          });
          this.addAPI();
        } else {
          jmodal.toast(data.msg);
        }
      });
    },
    
    //http同步APi
    httpSynchroApi(api, key){

      japi.rsaApi(api, key, (rsa)=>{
        var param = {'apiData':rsa};
        jhttps.postX("assetsV2/synchronizationSingelApi",param,true,(data)=>{

          if(data.status == 200){

            data = data.data;
            if (!data.apiId) {
              jmodal.hideLoad();
              jmodal.toast(T('API同步失败'));
              return;
            }

            var assets = data.apiId;
            if (assets.isExpire) {
              jmodal.hideLoad();
              jmodal.toast(T('API无效'));
              return
            }

            this.httpApiAccountId(api, assets, rsa);
          } else {
            jmodal.hideLoad();
            jmodal.toast(data.msg);
          }
        });
      });
    },
    
    //http获取账户标识
    httpApiAccountId(api, assets, rsa){

      var param = {'apiData':rsa};
      jhttps.postX("assetsV2/getApiAccountId", param,true,(data)=>{
        if(data.status == 200){
          var account = data.data;

          //未录入API：走添加逻辑
          if (account.states == 'NORMAL') {
            this.httpAddApi(api, assets, account);
          }
          
          /*1、已录入API，且有策略执行：询问后走续期逻辑
            2、已录入API，且无策略执行：询问后走续添加逻辑*/
          else if (account.states == 'BIND' || account.states == 'UNBIND') {
            
            var parm = {
                  'message':T("您已经开通过该账户，确认更新API").replace("&{1}", account.apiName)};
            jmodal.confirm(parm, (bool)=>{
              if (bool == "true") {
                account.states == 'BIND' ? this.renewalApi(api, assets, account) : this.httpAddApi(api, assets, account);
              } else {
                jmodal.hideLoad();
              }
            });
          } else {
            jmodal.hideLoad();
            jmodal.toast(T('获取API标识失败'));
          }
        } else {
          jmodal.hideLoad();
          jmodal.toast(data.msg);
        }
      });
    },
    
    //http添加API
    httpAddApi(api, assets, account) {
      
      var param = {'exchangeId':api.exchangeId,
                  'exchangeName':api.exchangeName,
                  'accountId':account.accountId};
      jhttps.postX("assetsV3/addUserApi",param,true,(data)=>{
        if(data.status == 200){
          var data = data.data;

          //基础数据+API数据+资产数据+账户数据
          var timestamp =  new Date().getTime();
          data.importTime = timestamp; 
          data.synchroTime = timestamp;
          var model = {...data, ...api, ...assets, ...account};
          japi.addApi(model, ()=>{
            jmodal.hideLoad();
            jmodal.toast(T('导入成功'));
            jnavigator.dismiss();
            jnavigator.back(1);
          });
        } else {
          jmodal.hideLoad();
          jmodal.toast(data.msg);
        }
      });
    },
    
    //http续期API
    httpRenewalApi(api, assets, account, key) {
      
      japi.rsaApi(api, key, (rsa)=>{
        var param = {'apiId':account.apiId,
                    'apiData':rsa};
        jhttps.postX("assetsV2/apiRenewal",param,true,(data)=>{
          if(data.status == 200){
            
            //API数据+资产数据+账户数据
            var timestamp =  new Date().getTime();
            api.importTime = timestamp; 
            api.synchroTime = timestamp;
            var model = {...api, ...assets, ...account};
            japi.addApi(model, ()=>{
              jmodal.hideLoad();
              jmodal.toast(T('导入成功'));
              jnavigator.dismiss();
              jnavigator.back(1);
            });
          } else {
            jmodal.hideLoad();
            jmodal.toast(data.msg);
          }
        });
      });
    },
    /********************************** 其他*/
    //添加API
    addAPI() {
      
      var api = JSON.parse(JSON.stringify(this.api));
      jmodal.showLoad();
      japi.httpPublicKey('api', false, (publicKey)=>{
        this.httpSynchroApi(api, publicKey);
      }, (msg)=>{
        jmodal.hideLoad();
        jmodal.toast(msg);
      });
    },

    //续期API
    renewalApi(api, assets, account) {
      
      japi.httpPublicKey('api', true, (publicKey)=>{
        this.httpRenewalApi(api, assets, account, publicKey);
      }, (msg)=>{
        jmodal.hideLoad();
        jmodal.toast(msg);
      });
    },
    
    /********************************** 点击触发*/
    //开通交易协议
    opJyClick() {
    },

    //开通量化协议
    opLHClick() {
    },
    
    //取消
    cancelClick(){
      jnavigator.dismiss();
    },

    //确定点击
    okClick(){ 
      if (!this.isOk) {
        jmodal.toast('请阅读协议');
        return;
      }
      this.httpCrerateAddress();
    },
  }
}

</script>

<style src="@/_css/style.css" ></style>
<style scoped>
/******************************************************* 手续费*/
.bg {
  flex-direction: row;
  justify-content: center;
  align-items: center;
  width: 750px;
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;

  background-color: rgba(0, 0, 0, 0.25);
}
.content {
  overflow: hidden;
  flex-direction: column;
  width: 600px;
  background: #202B46;
  border-radius: 24px;
  padding: 40px;
}
.title {
  text-align: center;
  margin-top: 10px;
  margin-bottom: 50px;
}
.text {
  line-height: 50px;
  margin-bottom: 20px;
}
.label {
  line-height: 50px;
  margin-bottom: 4px;
}
.agrees {
  flex-direction: row;
  margin-bottom: 40px;
  margin-top: 40px;
}

.img-agree {
  width: 34px;
  height: 34px;
  margin-right: 20px;
}

.buts {
  flex-direction: row;
  margin-left: -40px;
  margin-right: -40px;
  margin-bottom: -40px;

  border-top-width: 1px;
  border-color: #151A2A;
}
.but{
  flex-direction: row;
  justify-content: center;
  align-items: center;
  flex: 1;
  height: 88px;
}
.line {
  height: 88px;
  width: 1px;
  background-color: #151A2A;
}


.height40 {
  height: 40px;
}
.height60 {
  height: 60px;
}
</style>